'use strict'

//获取物品类型
exports.getItemMainType = function(templateId) {
  return Math.floor(templateId / 10000)
}

//获取物品子类型
exports.getItemSubType = function(templateId) {
  return Math.floor(templateId % 10000 / 100)
}

//获得随机数字
exports.getRandomNumber = function(min, max) {
  max = Math.floor(max)
  min = Math.floor(min)
  let n = max + min
  let x1 = Math.floor(Math.random() * (max - min + 1) + min)
  let x2 = Math.floor(Math.random() * (max - min + 1) + min)
  let x3 = x1 + x2
  let m = x3 > n ? x3 : 2 * n - x3
  return m - max
}

exports.addRandomHalf2 = function(value) {
  let max = Math.ceil(value + Math.random() * value / 5)
  let min = Math.ceil(value)
  if (max === min) {
    max += 1
  }
  let n = max + min
  let x1 = Math.floor(Math.random() * (max - min + 1) + min)
  let x2 = Math.floor(Math.random() * (max - min + 1) + min)
  let x3 = x1 + x2
  let m = x3 > n ? x3 : 2 * n - x3
  return m - max
}

//获得随机一组不重复的数字
exports.getRandomArray = function(num, array) {
  let arrayCopy = array.slice(0)
  let length = arrayCopy.length
  var k = []
  for (var i = 0; i < num; i++) {
    var l = Math.floor(Math.random() * length);
    length = length - 1
    k.push(arrayCopy[l])
    arrayCopy[l] = arrayCopy[length]
  }
  return k
}

//从一个数组里面随机获得其中一个
exports.getOneFromArray = function(array) {
  return array[Math.floor(Math.random() * array.length)]
}

//从一个数组里面随机获得其中一个
exports.addRandomHalf = function(value) {
  return value + Math.ceil(Math.random() * value / 3)
}

exports.getIndexByLucky = function(lucky, luckyRate, rateArray) {
  let p = calcLuckyRate(lucky, luckyRate)
  let array = calcRateArray(p, rateArray)
  let index = getIndexByRate(array)
  return index
}

//获得幸运率
exports.calcLuckyRate = calcLuckyRate

function calcLuckyRate(lucky, luckyRate) {
  let min = luckyRate[0]
  let max = luckyRate[1]
  let ratio = luckyRate[2]
  if (lucky <= min) {
    return 0
  }
  if (lucky > min && lucky <= max) {
    return (lucky - min) * ratio
  }
  if (lucky > max) {
    return max * ratio + lucky - max
  }
}

//根据幸运率返回概率数组
exports.calcRateArray = calcRateArray

function calcRateArray(p, rateArray) {
  let rate = rateArray.slice()
  let t = rate[0] + Math.floor(rate[0] / 7) * 4 + rate[1]
  if (p > t) {
    p = t
  }
  let p1 = p
  let p2 = 0

  if (p > rate[0]) {
    p1 = rate[0]
    p2 = p - rate[0]
  }

  if (!rate[2]) {
    rate[0] -= p1
    rate[1] += p1
  } else if (!rate[3]) {
    rate[0] -= p1
    let x = Math.floor(p1 / 3)
    let y = p1 % 3
    rate[1] += 2 * x + y
    rate[2] += x
    if (p2) {
      rate[1] -= p2
      rate[2] += p2
    }
  } else if (rate[3]) {
    rate[0] -= p1
    let x = Math.floor(p1 / 7)
    let y = p1 % 7
    if (y <= 4) {
      rate[1] += 4 * x + y
      rate[2] += 2 * x
    } else {
      rate[1] += 4 * x + 4
      rate[2] += 2 * x + y - 4
    }
    rate[3] += x
    if (p2) {
      rate[1] -= p2
      x = Math.floor(p2 / 3)
      y = p2 % 3
      rate[2] += 2 * x + y
      rate[3] += x
    }
  }
  return rate
}

//根据随机概率获得序号
exports.getIndexByRate = getIndexByRate

function getIndexByRate(rateArray) {
  let random = Math.floor(Math.random() * 100)
  if (random < rateArray[0]) {
    return 0
  }
  random -= rateArray[0]
  if (random < rateArray[1]) {
    return 1
  }
  random -= rateArray[1]
  if (random < rateArray[2]) {
    return 2
  }
  return 3
}
